package com.dream.utils.resource.teamplate;

import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import com.dream.utils.log.DreamLogUtils;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

/**
 * STMS 2012
 * 
 * @author Evan
 * @date 2012-09-15
 * 
 */
public class TeamplateUtils {
	
	private static final String DEFAULT_ENCODING = "UTF-8";
	private static  Configuration configuration;
	static{
		
		configuration = new Configuration();
		configuration.setNumberFormat("0.######");
		configuration.setDefaultEncoding(DEFAULT_ENCODING);
		configuration.setClassForTemplateLoading(TeamplateUtils.class, "/");
	}
	
	/**
	 * get template content
	 * @param teamplateName
	 * @param model
	 * @return
	 */
	public static <T> String getContent(String teamplateName,T model) {
		
		String content = null;
		try {
			Template template = configuration.getTemplate(teamplateName);
			content = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
			
		} catch (TemplateException e) {
			DreamLogUtils.error(TeamplateUtils.class, "getContent", "", e);
			throw new RuntimeException(e);
		} catch (Exception e) {
			DreamLogUtils.error(TeamplateUtils.class, "getContent", "", e);
			throw new RuntimeException(e);
		}
		return content;
	}
}
